//**********************hierarchy module************************************

//adminCreate

var actionCount_adminCreate = 0;
function createHierarchyAction(action){
	form = document.forms['hierarchyCreateForm'];
	if(actionCount_adminCreate == 0){
        if(action =='submit'){
          if(!confirm("Confirm Create")){
              return false;
          }
        }
		if(action =='reset'){
			form.createHierarchyName.value="";	
		}	
		actionCount_adminCreate = actionCount_adminCreate + 1;
		form.action.value = action;
		form.submit();
		return false;
	}
	return false;
}	

//adminDelete

var actionCount_adminDelete = 0;
function actionDeleteHierarchy(action) {
	form = document.forms['hierarchyAdminForm'];
	if(actionCount_adminDelete == 0){
		if(action == 'hierarchyType' || action == 'reset'){
			actionCount_adminDelete = actionCount_adminDelete + 1;
			form.action.value = action;
			form.submit();
		}
		
		if(action == 'submit' && form.hierarchy.value != '-1'){
			if(confirm(form.message0.value+"\n"+form.message1.value+"\n"+form.message2.value)){
				actionCount_adminDelete = actionCount_adminDelete + 1;
				form.action.value = action;
				form.submit();
			}
			return false;
		}
	}
	return false;
}

//adminDownload

var actionCount_adminDownload = 0;
function reloadAdminDownload(action) {
	form = document.forms['hierarchyAdminForm'];
    if(actionCount_adminDownload ==0){
        if(action == 'download'){
            form.action.value = "download";
        }else if(actionCount_adminDownload == 0){
            form.newHierarchyName.value = "";
            form.action.value = action;
			actionCount_adminDownload = actionCount_adminDownload + 1;
        }       
        form.submit();
        return false;
    }
	return false;
}

//adminExpire

var actionCount_adminExpire = 0;
function actionExpireHierarchy(action) {
	form = document.forms['hierarchyAdminForm'];
	if(actionCount_adminExpire == 0){
        if(action == 'expire'){
            if(!confirm("Confirm Expire")){
                return false;
            }
        }

		actionCount_adminExpire = actionCount_adminExpire + 1;
		if(action == 'hierarchyType'){
			form.action.value = action;
			form.submit();
			return false;
		}else{
			if(form.hierarchy.value == '-1' || form.period.value == '-1'){
				actionCount_adminExpire = 0;
				return false;
			}else{
				form.action.value = action;
				form.submit();
				return false;			
			}
		}
		
	}
	return false;
}

function goToAdminTreeAction(action, nodeId){
	document.hierarchyAdminForm.action.value = action;
	document.hierarchyAdminForm.nodeId.value = nodeId;
	document.hierarchyAdminForm.submit();
	return false;
}

//adminHierarchyCriteria

var actionCount_adminHierarchyCriteria = 0;
function gotoAdminHierarchyCriteriaURL(action){
	form = document.forms['hierarchyCopyForm'];
	form1= document.forms['copyDetailsForm'];
	if(actionCount_adminHierarchyCriteria == 0){
		actionCount_adminHierarchyCriteria = actionCount_adminHierarchyCriteria + 1;
		if(action == 'hierarchyType'){
			form.action.value = action;
			form.submit();
			return false;
		}else{
			if(form.hierarchy.value == '-1' || form.period.value == '-1'){
				actionCount_adminHierarchyCriteria = 0;
				return false;
			}else{
				form.action.value = action;
				if(action == 'copySubmit'){
					form.copyHierarchyName.value = form1.copyHierarchyName.value;
					form.copyHierarchyDescription.value = form1.copyHierarchyDescription.value;
				}
				if(action == 'copyReset'){
					form.copyHierarchyName.value="";
					form1.copyHierarchyName.value="";
					form.copyHierarchyDescription.value="";
					form1.copyHierarchyDescription.value="";
				}				
				form.submit();
			}
			
			return false;
		}


	}
	return false;
	return false;
}

function goToAdminTreeActionCriteria(action, nodeId){
	form = document.forms['hierarchyCopyForm'];
	form.action.value = action;
	form.nodeId.value = nodeId;
	form.submit();
	return false;
}

//adminHierarchyTree
	//goToAdminTreeActionCriteria(action,nodeId)

//adminImport.java

var actionCount_adminImport = 0;
function reloadAdminImport(action){
    form = document.forms['hierarchyAdminForm'];
	if(actionCount_adminImport == 0){
        if(action == 'submit'){
            if(!confirm("Confirm Import")){
                return false;
            }
        }
		actionCount_adminImport = actionCount_adminImport + 1;
		form.action.value = action;
		form.submit();
		return false;
	}
	return false;
}

//adminPublish

var actionCount_adminPublish = 0;
function reloadAdminPublish(action){
	form = document.forms['hierarchyAdminForm'];
	if(actionCount_adminPublish == 0){
        if(action == 'publish'){
            if(!confirm("Confirm Publish")){
                return false;
            }
        }
	    actionCount_adminPublish = actionCount_adminPublish + 1;
		form.action.value = action;
		form.submit();
		return false;
	}
	return false;
}

//compareCriteria

var actionCount_compareCriteria = 0;
function gotoCompareCriteriaURL(action){
	form =document.forms['hierarchyForm'];
	if(actionCount_compareCriteria == 0){
		actionCount_compareCriteria = actionCount_compareCriteria + 1;
		if(action == 'hierarchyType'){
			form.action.value = action;
			form.submit();
			return false;
		}else if(action == 'hierarchy' || action == 'period' || action == 'submit' || action == 'reset'){
			if(form.hierarchy.value == '-1' || form.period.value == '-1'){
				actionCount_compareCriteria = 0;
			}else{
				if(form.period.value != form.selectedPeriod1.value)
					form.action.value = 'period';
				else
					form.action.value = action;
				form.submit();
			}
			return false;
		}
	}
	return false;
}

//comparePeriod1Tree

function goToHierarchyManageAction(action, nodeId){
	form = document.forms['hierarchyManageForm'];
	//form1 = document.forms['reportBody'];
	form.action.value = action;
	form.nodeId.value = nodeId;
	if(document.forms['reportBody'] != null){
		var selectedElementsList = "";
		var count = 0;
		for(var i=0; i < document.forms['reportBody'].elements.length; i++){
			if(document.forms['reportBody'].elements[i].type == "hidden" && document.forms['reportBody'].elements[i].value == "true"){
				if(count > 0)
					selectedElementsList += "#";
				selectedElementsList += document.forms['reportBody'].elements[i].name;
				count++;
			}
		}
		form.selectedElementsList.value = selectedElementsList;
	}
	form.submit();
	return false;

}

//comparePeriod2Tree
function goToCompareTreeAction(action, nodeId){
	form = document.forms['hierarchyPeriodCompareForm'];
	form.action.value = action;
	form.nodeId.value = nodeId;
	form.submit();
	return false;
}

//expireHierarchyTree

//hierarchyCriteria

var actionCount_hierarchyCriteria = 0;
function gotoHierarchyCriteriaURL(action){

	if(actionCount_hierarchyCriteria == 0){
		actionCount_hierarchyCriteria = actionCount_hierarchyCriteria + 1;
		if(action == 'hierarchyType' || action == 'hierarchy'){
			document.forms['hierarchyForm'].action.value = action;
			document.forms['hierarchyForm'].moveTreeSelectedNodesList.value = "";
			document.forms['hierarchyForm'].submit();
			return false;
		}else{
			if(document.forms['hierarchyForm'].hierarchy.value == '-1' || document.forms['hierarchyForm'].period.value == '-1'){
				actionCount_hierarchyCriteria = 0;
				return false;
			}else{
				if(action == 'element' || action == 'period'){
					document.forms['hierarchyForm'].action.value = action;
					if(document.forms['manageMoveForm'] != null && action == 'element')
						document.forms['hierarchyForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value;
					if(action == 'period')
						document.forms['hierarchyForm'].moveTreeSelectedNodesList.value = "";
					document.forms['hierarchyForm'].submit();
					return false;
				}else if(action == 'reset' || action == 'submit'){
					document.forms['hierarchyForm'].action.value = action;
					if(document.forms['manageMoveForm'] != null && action == 'submit')
						document.forms['hierarchyForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value;
					if(action == 'reset')
						document.forms['hierarchyForm'].moveTreeSelectedNodesList.value = "";
					document.forms['hierarchyForm'].submit();
					return false;
				}else if(action == 'group'){
					if(document.forms['hierarchyManageForm'].newSelect.value == -1){
						actionCount_hierarchyCriteria = 0;
					}else{
						document.forms['hierarchyManageForm'].action.value = action;
						if(document.forms['manageMoveForm'] != null)
							document.forms['hierarchyManageForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value;
						document.forms['hierarchyManageForm'].submit();
					}
					return false;
				}else if(action == 'resetGroup' || action == 'createGroup'){
					if(action == 'resetGroup'){
						document.forms['manageGroupForm'].groupName.value = "";
						document.forms['manageGroupForm'].groupId.value="";
					}				
					document.forms['manageGroupForm'].action.value = action;
					document.forms['manageGroupForm'].submit();
					return false;
				}else if(action == 'detailsRemove' || action == 'detailsReset' || action == 'detailsUpdate' || action == 'detailsCancel' || action == 'detailsRemoveConfirm'){
					document.forms['manageDetailsForm'].action.value = action;
					document.forms['manageDetailsForm'].submit();
					return false;
				}else if(action == 'pre_elementRemove' || action == 'elementAdd' || action == 'elementReset' || action == 'elementRemove' || action == 'elementCancel'){
					document.forms['reportBody'].action.value = action;
					document.forms['reportBody'].submit();
					return false;
				}else if(action == 'moveReset' || action == 'moveSubmit' || action == 'moveOK'){
					document.forms['manageMoveForm'].action.value = action;
					document.forms['manageMoveForm'].submit();
					return false;
				}
			}
		}

	}
	return false;
}

//hierarchySubMenu

function gotoTabURL(action){
	if(action == 'manage' || action == 'admin' || action == 'compare' || action == 'search'){
		form = document.forms['hierarchyTabForm'];
form.panelName.value = action;
		form.actionTrack.value = action;
		if(document.forms['manageMoveForm'] != null)
			form.moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value;	
			document.forms['hierarchyTabForm'].submit();
		}
}

//hierarchyTree

function goToHierarchyTreeManageAction(action, nodeId){
	form = document.forms['hierarchyManageForm'];
	form.action.value = action;
	form.nodeId.value = nodeId;
	if(document.forms['reportBody'] != null){
		var selectedElementsList = "";
		var count = 0;
		for(var i=0; i < document.forms['reportBody'].elements.length; i++){
			if(document.forms['reportBody'].elements[i].type == "hidden" && document.forms['reportBody'].elements[i].value == "true"){
				if(count > 0)
					selectedElementsList += "#";
				selectedElementsList +=  document.forms['reportBody'].elements[i].name;
				count++;
			}
		}
		form.selectedElementsList.value = selectedElementsList;
	}
	if(document.forms['manageMoveForm'] != null)
		form.moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value;
	form.submit();
//	return false;

}

//manageMove

/*function maintainCheckBoxes(nodeId, status){
	var blankImg = "<edocs:urlPrefix/>/_assets/images/blankSpace.gif";
	document.manageMoveForm.moveTreeNodesStatus.value = "changed";
	if(status == true){
		if(document.manageMoveForm.moveTreeSelectedNodesList.value != ""){
			var selectedNodesList = document.manageMoveForm.moveTreeSelectedNodesList.value.split("#");
			document.manageMoveForm.moveTreeSelectedNodesList.value = "";
			for(var index=0; index < selectedNodesList.length; index++){
				var currentCheckingNode = selectedNodesList[index];
				if(currentCheckingNode.search(nodeId+".") == -1){
					if(document.manageMoveForm.moveTreeSelectedNodesList.value == "")
						document.manageMoveForm.moveTreeSelectedNodesList.value = currentCheckingNode;
					else
						document.manageMoveForm.moveTreeSelectedNodesList.value = document.manageMoveForm.moveTreeSelectedNodesList.value + "#" + currentCheckingNode;
				}
			}

			if(document.manageMoveForm.moveTreeSelectedNodesList.value != "")
				document.manageMoveForm.moveTreeSelectedNodesList.value = document.manageMoveForm.moveTreeSelectedNodesList.value + "#";
		}
		for(var count=0; count < document.manageMoveForm.moveNodes.length; count++){
			if(document.manageMoveForm.moveNodes[count].value.search(nodeId+".") != -1){
				document.manageMoveForm.moveNodes[count].checked = true;
				document.manageMoveForm.moveNodes[count].disabled = true;
			}
		}
		for(var imgCount=0; imgCount < document.images.length; imgCount++){
			if(document.images[imgCount].name == nodeId+"Img"){
				document.images[imgCount].src =  blankImg;
				document.images[imgCount].alt = "";
			}
		}		
		document.manageMoveForm.moveTreeSelectedNodesList.value = document.manageMoveForm.moveTreeSelectedNodesList.value + nodeId;
	}else{
		var selectedNodesList = document.manageMoveForm.moveTreeSelectedNodesList.value.split("#");
		document.manageMoveForm.moveTreeSelectedNodesList.value = "";
		for(var index=0; index < selectedNodesList.length; index++){
			var currentCheckingNode = selectedNodesList[index];
			if(currentCheckingNode.search(nodeId) == -1){
				if(document.manageMoveForm.moveTreeSelectedNodesList.value == "")
					document.manageMoveForm.moveTreeSelectedNodesList.value = currentCheckingNode;
				else
					document.manageMoveForm.moveTreeSelectedNodesList.value = document.manageMoveForm.moveTreeSelectedNodesList.value + "#" + currentCheckingNode;				
			}
		}
		
		for(var count=0; count < document.manageMoveForm.moveNodes.length; count++){
			if(document.manageMoveForm.moveNodes[count].value.search(nodeId+".") != -1){
				document.manageMoveForm.moveNodes[count].checked = false;
				document.manageMoveForm.moveNodes[count].disabled = false;
			}
		}
		
	}
	return false;
}

function goToMovePanel(status, nodeId){

	document.manageMoveForm.action.value = status;
	document.manageMoveForm.moveNodeId.value = nodeId;
	document.manageMoveForm.submit();
	return false;

}
function goToTabPanel(action){
	if(action == 'details'){
		document.manageDetailsForm.moveTreeSelectedNodesList.value = document.manageMoveForm.moveTreeSelectedNodesList.value;
		document.manageDetailsForm.submit();
	}
	if(action == 'elements'){
		document.manageElementsForm.moveTreeSelectedNodesList.value = document.manageMoveForm.moveTreeSelectedNodesList.value;
		document.manageElementsForm.submit();
	}
	return false;
}
*/ 
//Some javascript codes in this page are left , since those are not within functions
function toggleVisibility(obj,buttonName,textBefore,textAfter) {
	var button = "input[name="+buttonName+"]";
	if ($("#"+obj).hasClass("displayNone")) {
		$("#"+obj).removeClass("displayNone");
		$("#"+obj).addClass("displayBlock");
		$(button).attr('value',textAfter);	
	}
	else {
		$("#"+obj).removeClass("displayBlock");
		$("#"+obj).addClass("displayNone");
		$(button).attr('value',textBefore);
	}
}


var actionCount_hierarchyCriteria = 0;
function gotoCriteriaURL(action){
	if(action == 'element' || action == 'reset'){
		document.forms['hierarchyForm'].filtering.value = 'true';
	}
	
	if(actionCount_hierarchyCriteria == 0){
		actionCount_hierarchyCriteria = actionCount_hierarchyCriteria + 1;
		if(action == 'hierarchyType' || action == 'hierarchy'){
			document.forms['hierarchyForm'].action.value = action;
			document.forms['hierarchyForm'].moveTreeSelectedNodesList.value = "";
			document.forms['hierarchyForm'].submit();
			return false;
		}else{
			if(document.forms['hierarchyForm'].hierarchy.value == '-1' || document.forms['hierarchyForm'].period.value == '-1'){
				actionCount_hierarchyCriteria = 0;
				return false;
			}else{
				if(action == 'element' || action == 'period'){
					document.forms['hierarchyForm'].action.value = action;
					if(document.forms['manageMoveForm'] != null && action == 'element')
						document.forms['hierarchyForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value;
					if(action == 'period')
						document.forms['hierarchyForm'].moveTreeSelectedNodesList.value = "";
					document.forms['hierarchyForm'].submit();
					return false;
				}else if(action == 'reset' || action == 'submit'){
					document.forms['hierarchyForm'].action.value = action;
					if(document.forms['manageMoveForm'] != null && action == 'submit')
						document.forms['hierarchyForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value;
					if(action == 'reset')
						document.forms['hierarchyForm'].moveTreeSelectedNodesList.value = "";
					document.forms['hierarchyForm'].submit();
					return false;
				}else if(action == 'group'){
					if(document.forms['hierarchyManageForm'].newSelect.value == -1){
						actionCount_hierarchyCriteria = 0;
					}else{
						document.forms['hierarchyManageForm'].action.value = action;
						if(document.forms['manageMoveForm'] != null)
							document.forms['hierarchyManageForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value;
//						loadRightPanel('../hierarchy/manageGroup.action', action);
						document.forms['hierarchyManageForm'].submit();
					}
					return false;
				}else if(action == 'resetGroup' || action == 'createGroup'){
					if(action == 'resetGroup'){
						document.forms['manageGroupForm'].groupName.value = "";
						document.forms['manageGroupForm'].groupId.value="";
					}				
					document.forms['manageGroupForm'].action.value = action;
					document.forms['manageGroupForm'].submit();
					return false;
				}else if(action == 'detailsRemove' || action == 'detailsReset' || action == 'detailsUpdate' || action == 'detailsCancel' || action == 'detailsRemoveConfirm'){
					document.forms['manageDetailsForm'].action.value = action;
					document.forms['manageDetailsForm'].submit();
					return false;
				}else if(action == 'pre_elementRemove' || action == 'elementAdd' || action == 'elementReset' || action == 'elementRemove' || action == 'elementCancel'){
					document.forms['reportBody'].action.value = action;
					document.forms['reportBody'].submit();
					return false;
				}else if(action == 'moveReset' || action == 'moveSubmit' || action == 'moveOK'){
					document.forms['manageMoveForm'].action.value = action;
					document.forms['manageMoveForm'].submit();
					return false;
				}
			}
		}

	}
	return false;
}


function maintainCheckBoxes(nodeId, status){
	var blankImg = "<edocs:urlPrefix/>/_assets/images/blankSpace.gif";
	document.forms['manageMoveForm'].moveTreeNodesStatus.value = "changed";
	if(status == true){
		if(document.forms['manageMoveForm'].moveTreeSelectedNodesList.value != ""){
			var selectedNodesList = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value.split("#");
			document.forms['manageMoveForm'].moveTreeSelectedNodesList.value = "";
			for(var index=0; index < selectedNodesList.length; index++){
				var currentCheckingNode = selectedNodesList[index];
				if(currentCheckingNode.indexOf(nodeId+".") == -1){
					if(document.forms['manageMoveForm'].moveTreeSelectedNodesList.value == "")
						document.forms['manageMoveForm'].moveTreeSelectedNodesList.value = currentCheckingNode;
					else
						document.forms['manageMoveForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value + "#" + currentCheckingNode;
				}
			}

			if(document.forms['manageMoveForm'].moveTreeSelectedNodesList.value != "")
				document.forms['manageMoveForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value + "#";
		}
		for(var count=0; count < document.forms['manageMoveForm'].moveNodes.length; count++){
			if(document.forms['manageMoveForm'].moveNodes[count].value.indexOf(nodeId+".") != -1){
				document.forms['manageMoveForm'].moveNodes[count].checked = true;
				document.forms['manageMoveForm'].moveNodes[count].disabled = true;
			}
		}
		for(var imgCount=0; imgCount < document.images.length; imgCount++){
			if(document.images[imgCount].name == nodeId+"Img"){
				document.images[imgCount].src =  blankImg;
				document.images[imgCount].alt = "";
			}
		}		
		document.forms['manageMoveForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value + nodeId;
	}else{
		var selectedNodesList = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value.split("#");
		document.forms['manageMoveForm'].moveTreeSelectedNodesList.value = "";
		for(var index=0; index < selectedNodesList.length; index++){
			var currentCheckingNode = selectedNodesList[index];
			if(currentCheckingNode.indexOf(nodeId) == -1){
				if(document.forms['manageMoveForm'].moveTreeSelectedNodesList.value == "")
					document.forms['manageMoveForm'].moveTreeSelectedNodesList.value = currentCheckingNode;
				else
					document.forms['manageMoveForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value + "#" + currentCheckingNode;				
			}
		}
		
		for(var count=0; count < document.forms['manageMoveForm'].moveNodes.length; count++){
			if(document.forms['manageMoveForm'].moveNodes[count].value.indexOf(nodeId+".") != -1){
				document.forms['manageMoveForm'].moveNodes[count].checked = false;
				document.forms['manageMoveForm'].moveNodes[count].disabled = false;
			}
		}
		
	}
	return false;
}

function goToMovePanel(status, nodeId){

	document.forms['manageMoveForm'].action.value = status;
	document.forms['manageMoveForm'].moveNodeId.value = nodeId;
	document.forms['manageMoveForm'].submit();
	return false;

}
function goToTabPanel(action){
	if(action == 'details'){
		document.forms['manageDetailsForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value;
//		document.forms['manageDetailsForm'].submit();
		loadRightPanel('../hierarchy/manageDetails.action', action);
	}
	if(action == 'elements'){
		document.forms['manageElementsForm'].moveTreeSelectedNodesList.value = document.forms['manageMoveForm'].moveTreeSelectedNodesList.value;
//		document.forms['manageElementsForm'].submit();
		loadRightPanel('../hierarchy/manageElements.action', action);
	}
//	return false;
}


var actionCount = 0;
function gotoAdminCriteriaURL(action){
	form = document.forms['hierarchyCopyForm'];
	form1 = document.forms['copyDetailsForm'];
	if(actionCount == 0){
		actionCount = actionCount + 1;
		if(action == 'hierarchyType'){
			form.action.value = action;
			form.submit();
			return false;
		}else{
			if(form.hierarchy.value == '-1' || form.period.value == '-1'){
				actionCount = 0;
				return false;
			}else{
				form.action.value = action;
				if(action == 'copySubmit'){
                    if(confirm("Confirm Copy")){
                        form.copyHierarchyName.value = form1.copyHierarchyName.value;
                        form.copyHierarchyDescription.value = form1.copyHierarchyDescription.value;
                        form.submit();
                    }else{
                        actionCount = 0;
                    }
                    return false;
				}else if(action == 'copyReset'){
					form.copyHierarchyName.value="";
					form1.copyHierarchyName.value="";
					form.copyHierarchyDescription.value="";
					form1.copyHierarchyDescription.value="";
                    actionCount = 0;
                    return false;
				}
                    form.submit();
                    return false;

			}
			

		}
	}
	return false;
}

function goToAdminTreeActionCriteria(action, nodeId){
	form = document.forms['hierarchyCopyForm'];
	form.action.value = action;
	form.nodeId.value = nodeId;
	form.submit();
	return false;
}


function toggleTree(node){

    node = "#li"+node.substring(4).replace(/[^a-zA-Z 0-9]/g,'t');
    treeviewApp.toggleGroupById(node);
}

function selectTreeElement(nodeId){
	myurl = "../hierarchy/ajaxHierarchy.action";
	var liNodeId = "#li" + nodeId.substring(4).replace(/[^a-zA-Z 0-9]/g,'t');
    $("li"+liNodeId).focus();
	if(document.forms['reportBody'] != null){
		var selectedElementsList = "";
		var count = 0;
		for(var i=0; i < document.forms['reportBody'].elements.length; i++){
			if(document.forms['reportBody'].elements[i].type == "hidden" && document.forms['reportBody'].elements[i].value == "true"){
				if(count > 0)
					selectedElementsList += "#";
				selectedElementsList +=  document.forms['reportBody'].elements[i].name;
				count++;
			}
		}
		$('div#rightPanel').load(myurl, {node:nodeId, task:'select', selectElementsList:selectedElementsList}, function(){
			$('ul#reportcontextlist li').eq(5).html(nodiId);
			$("li a").removeClass();
			$("li#"+liNodeId+" a").addClass("selected");
		});
	}
	else if(document.forms['manageMoveForm'] != null){
		goToHierarchyTreeManageAction('treeNodeSelect', nodeId);
	}
	else{
		$('div#rightPanel').load(myurl, {node:nodeId, task:'select'}, function(){
			if($('div#rightPanel').html().indexOf('j_username') > 1){
				window.location = "../login.action";
			}
			var nodeName = $(liNodeId + ' a').eq(1).text();
			if(nodeName==null || nodeName.length==0)
				nodeName = $(liNodeId + ' a').eq(0).text();
			$('ul#reportcontextlist li').eq(5).html(nodeName);
			$("li a").removeClass();
			$("li#"+liNodeId+" a").addClass("selected");
		});
	}
}

function loadRightPanel(url, action){
	$('div#rightPanel').load(url, {target:'rightpanel', action:action}, function(){
		if($('div#rightPanel').html().indexOf('j_username') > 1){
			window.location = "../login.action";
		}
	});
}


//Hierarchy Tree Start --------------------------------------------------------------------

function extend(child, parent){
    var F = function(){};
    F.prototype = parent.prototype;
    child.prototype = new F();
    child.prototype.constructor = child;
    child.uber = parent.prototype;
}

function treeview(treeID) {

    this.$id = $('#' + treeID);
    this.$items = this.$id.find('li');
    this.$parents = this.$id.find('.tree-parent');
    this.$visibleItems = null;
    this.$activeItem = null;

    this.keys = {
        tab:      9,
        enter:    13,
        space:    32,
        left:     37,
        up:       38,
        right:    39,
        down:     40
    };

    this.init();

    this.bindHandlers();
}


treeview.prototype.init = function() {

    // If the aria-expanded is false, hide the group and display the collapsed state image
    this.$parents.each(function() {
        if ($(this).attr('aria-expanded') == 'false') {
            $(this).children('ul').hide().attr('aria-hidden', 'true');
        }else if($(this).attr('aria-expanded') == 'true'){
            $(this).children('ul').show().attr('aria-hidden', 'false');
        }
    });

    this.$visibleItems = this.$id.find('li:visible');
}


treeview.prototype.reload = function($item) {

    this.$items = this.$id.find('li');
    this.$parents = this.$id.find('.tree-parent');
    this.$visibleItems = null;
    this.$activeItem = null;

    this.init();
    this.bindItemHandlers($item);
}


treeview.prototype.bindItemHandlers = function($item) {

    var thisObj = this;

    // bind a click handler
    //this.$items.click(function(e) {
    //    return thisObj.handleClick($(this), e);
    //});

    // bind a keydown handler
    $item.find('li').keydown(function(e) {
        return thisObj.handleKeyDown($(this), e);
    });

    // bind a focus handler
    $item.find('li').focus(function(e) {
        return thisObj.handleFocus($(this), e);
    });
}


treeview.prototype.bindHandlers = function() {

    var thisObj = this;

    // bind a click handler
    //this.$items.click(function(e) {
    //    return thisObj.handleClick($(this), e);
    //});

    // bind a keydown handler
    this.$items.keydown(function(e) {
        return thisObj.handleKeyDown($(this), e);
    });

    // bind a focus handler
    this.$items.focus(function(e) {
        return thisObj.handleFocus($(this), e);
    });

    // bind a document click handler
    $(document).click(function(e) {

        if (thisObj.$activeItem != null) {
            // remove the focus styling
            thisObj.$activeItem.removeClass('tree-focus');

            // set activeItem to null
            thisObj.$activeItem = null;
        }

        return true;
    });
}


treeview.prototype.expandGroup = function($item) {

    // find the first child ul node
    var $group = $item.children('ul');

    // expand the group
    $group.show().attr('aria-hidden', 'false');

    // set the aria-expanded property
    $item.attr('aria-expanded', 'true');

    // update the list of visible items
    this.$visibleItems = this.$id.find('li:visible');
}


treeview.prototype.collapseGroup = function($item) {

    // find the first child ul node
    var $group = $item.children('ul');

    // collapse the group
    $group.hide().attr('aria-hidden', 'true');

    // update the aria-expanded property
    $item.attr('aria-expanded', 'false');

    // update the list of visible items
    this.$visibleItems = this.$id.find('li:visible');
}


treeview.prototype.toggleGroup = function($item, hasFocus) {

    var $group = $item.children('ul');

    myurl = "../hierarchy/ajaxHierarchy.action";

    var node = $item.attr("nodeId");
    var nodeID = node.substring(4).replace(/[^a-zA-Z 0-9]/g,'t');;

    if ($item.attr('aria-expanded') == 'true') {
        // collapse the group

        replaceImage(nodeID, '../_assets/images/loading_spinner.gif', spinIconLabel);

        if($("ul#ul"+nodeID).is(':visible')){
            //node is full, hide and let server know user collapsed it

            replaceImage(nodeID, '../_assets/images/expand.gif', expandIconLabel);
            $.get(myurl,{node: node, task: 'collapse'},function(){
                treeviewApp.collapseGroup($item);
                $item.focus();
            });
        }

    }else {
        // expand the group

        replaceImage(nodeID, '../_assets/images/loading_spinner.gif', spinIconLabel);

        // length less than 31 instead of string '<li tabindex="-1"></li>'
        if((jQuery.trim($("ul#ul"+nodeID).html()).length < 31)){
            //node is empty, need to go to server for content

            $("ul#ul"+nodeID).load(myurl,{'node': node, 'task': 'expand'}, function(){

                replaceImage(nodeID, '../_assets/images/collapse.gif', collapseIconLabel);
                treeviewApp.expandGroup($item);
                $item.focus();
            });

        }else{
            //node is full, show and let server know user expanded it

            replaceImage(nodeID, '../_assets/images/collapse.gif', collapseIconLabel);
            $.get(myurl,{node: node, task: 'quietexpand'}, function(){
                treeviewApp.expandGroup($item);
                $item.focus();
            });
        }
    }
}

treeview.prototype.toggleGroupById = function(node){
    var $jnode = this.$items.filter(node);
    this.toggleGroup($jnode);
}


treeview.prototype.updateStyling = function($item) {

    // remove the focus and highlighting from the treeview items
    // and remove them from the tab order.
    this.$items.removeClass('tree-focus').attr('tabindex', '-1');

    // apply the focus and styling and place the element in the tab order
    $item.addClass('tree-focus').attr('tabindex', '0');
}


treeview.prototype.handleKeyDown = function($item, e) {

    var curNdx = this.$visibleItems.index($item);

    if ((e.altKey || e.ctrlKey)
        || (e.shiftKey && e.keyCode != this.keys.tab)) {
        // do nothing
        return true;
    }

    switch (e.keyCode) {
        case this.keys.tab: {
            // set activeItem to null
            this.$activeItem = null;

            // remove the focus styling
            $item.removeClass('tree-focus');

            return true;
        }
        case this.keys.enter: {

            if(!$item.is('.tree-parent')){
                $item.children('a')[0].click();
            }else{
                $item.children('span').children('a')[1].click();
            }

        }
        case this.keys.space: {

            if (!$item.is('.tree-parent')) {
                // do nothing
            }
            else {
                // toggle the child group open or closed
                this.toggleGroup($item, true);
            }

            e.stopPropagation();
            return false;
        }
        case this.keys.left: {

            if ($item.is('.tree-parent') && $item.attr('aria-expanded') == 'true') {
                // collapse the group and return
                $item.children('span').children('a')[0].click();
            }
            else {
                // move up to the parent
                var $itemUL = $item.parent();
                var $itemParent = $itemUL.parent();

                // store the active item
                this.$activeItem = $itemParent;

                // set focus on the parent
                this.$activeItem.focus();
            }

            e.stopPropagation();
            return false;
        }
        case this.keys.right: {

            if (!$item.is('.tree-parent')) {
                // do nothing
            }
            else if ($item.attr('aria-expanded') == 'false') {
                $item.children('span').children('a')[0].click();
            }
            else {
                // move to the first item in the child group
                this.$activeItem = $item.children('ul').children('li').first();
                this.$activeItem.focus();
            }

            e.stopPropagation();
            return false;
        }
        case this.keys.up: {

            if (curNdx > 0) {
                var $prev = this.$visibleItems.eq(curNdx - 1);

                // store the active item
                this.$activeItem = $prev;

                $prev.focus();
            }

            e.stopPropagation();
            return false;
        }
        case this.keys.down: {

            if (curNdx < this.$visibleItems.length - 1) {
                var $next = this.$visibleItems.eq(curNdx + 1);

                // store the active item
                this.$activeItem = $next;

                $next.focus();
            }
            e.stopPropagation();
            return false;
        }
    }

    return true;
}


treeview.prototype.handleClick = function($id, e) {

    if (e.altKey || e.ctrlKey || e.shiftKey) {
        // do nothing
        return true;
    }

    // update the active item
    this.$activeItem = $id;

    // apply the focus highlighting
    this.updateStyling($id);

    e.stopPropagation();
    return false;
}


treeview.prototype.handleFocus = function($item, e) {

    if (this.$activeItem == null) {
        this.$activeItem = $item;
    }

    this.updateStyling(this.$activeItem);

    return true;
}

function HierarchyManagerTreeView(treeID){
    treeview.call(this,treeID);
}
extend(HierarchyManagerTreeView,treeview);

HierarchyManagerTreeView.prototype.toggleGroup = function($item, hasFocus) {

    var $group = $item.children('ul');

    myurl = "../hierarchy/ajaxHierarchy.action";

    var node = $item.attr("nodeId");
    var nodeID = node.substring(4).replace(/[^a-zA-Z 0-9]/g,'t');;

    var pattern = /\{\d+\}/g;
    var nodeName = $item.attr("nodeName");
    var imageAlt;

    if ($item.attr('aria-expanded') == 'true') {
        // collapse the group

        replaceImage(nodeID, '../_assets/images/loading_spinner.gif', spinIconLabel);

        if($("ul#ul"+nodeID).is(':visible')){
            //node is full, hide and let server know user collapsed it

            imageAlt = treeviewCollapsed;
            var match = pattern.exec(imageAlt);
            if (match){
                imageAlt = imageAlt.replace(match, nodeName);
            }

            replaceImage(nodeID, '../_assets/images/expand.gif', imageAlt);
            $.get(myurl,{node: node, task: 'collapse'},function(){
                treeviewApp.collapseGroup($item);
                $item.focus();
            });
        }

    }else {
        // expand the group

        replaceImage(nodeID, '../_assets/images/loading_spinner.gif', spinIconLabel);

        // length less than 31 instead of string '<li tabindex="-1"></li>'
        if((jQuery.trim($("ul#ul"+nodeID).html()).length < 31)){
            //node is empty, need to go to server for content

            $("ul#ul"+nodeID).load(myurl,{'node': node, 'task': 'expand'}, function(){

                imageAlt = treeviewExpanded;
                var match = pattern.exec(imageAlt);
                if (match){
                    imageAlt = imageAlt.replace(match, nodeName);
                }

                replaceImage(nodeID, '../_assets/images/collapse.gif', imageAlt);
                treeviewApp.reload($item);
                treeviewApp.expandGroup($item);
                $item.focus();

            });

        }else{
            //node is full, show and let server know user expanded it

            imageAlt = treeviewExpanded;
            var match = pattern.exec(imageAlt);
            if (match){
                imageAlt = imageAlt.replace(match, nodeName);
            }

            replaceImage(nodeID, '../_assets/images/collapse.gif', imageAlt);
            $.get(myurl,{node: node, task: 'quietexpand'}, function(){
                treeviewApp.expandGroup($item);
                $item.focus();
            });
        }

    }

}

HierarchyManagerTreeView.prototype.handleKeyDown = function($item, e) {

    var curNdx = this.$visibleItems.index($item);

    if ((e.altKey || e.ctrlKey)
        || (e.shiftKey && e.keyCode != this.keys.tab)) {
        // do nothing
        return true;
    }

    switch (e.keyCode) {
        case this.keys.tab: {
            // set activeItem to null
            this.$activeItem = null;

            // remove the focus styling
            $item.removeClass('tree-focus');

            return true;
        }
        case this.keys.enter: {

            if(!$item.is('.tree-parent')){
                $item.children('a')[0].click();
            }else{
                $item.children('a')[1].click();
            }

        }
        case this.keys.space: {

            if (!$item.is('.tree-parent')) {
                // do nothing
            }
            else {
                // toggle the child group open or closed
                this.toggleGroup($item, true);
            }

            e.stopPropagation();
            return false;
        }
        case this.keys.left: {

            if ($item.is('.tree-parent') && $item.attr('aria-expanded') == 'true') {
                // collapse the group and return
                this.toggleGroup($item, true);
            }
            else {
                // move up to the parent
                var $itemUL = $item.parent();
                var $itemParent = $itemUL.parent();

                // store the active item
                this.$activeItem = $itemParent;

                // set focus on the parent
                this.$activeItem.focus();
            }

            e.stopPropagation();
            return false;
        }
        case this.keys.right: {

            if (!$item.is('.tree-parent')) {
                // do nothing
            }
            else if ($item.attr('aria-expanded') == 'false') {
                this.toggleGroup($item, true);
            }
            else {
                // move to the first item in the child group
                this.$activeItem = $item.children('ul').children('li').first();
                this.$activeItem.focus();
            }

            e.stopPropagation();
            return false;
        }
        case this.keys.up: {

            if (curNdx > 0) {
                var $prev = this.$visibleItems.eq(curNdx - 1);

                // store the active item
                this.$activeItem = $prev;
                $prev.focus();
            }

            e.stopPropagation();
            return false;
        }
        case this.keys.down: {

            if (curNdx < this.$visibleItems.length - 1) {
                var $next = this.$visibleItems.eq(curNdx + 1);

                // store the active item
                this.$activeItem = $next;
                $next.focus();
            }
            e.stopPropagation();
            return false;
        }
    }

    return true;

}

function HierarchyMoveTreeView(treeID){
    treeview.call(this,treeID);
}
extend(HierarchyMoveTreeView,treeview);

function HierarchyCompareTreeView(treeID){
    treeview.call(this,treeID);
}
extend(HierarchyCompareTreeView,treeview);

function HierarchyCopyTreeView(treeID){
    treeview.call(this,treeID);
}
extend(HierarchyCopyTreeView,treeview);

function HierarchyExpireTreeView(treeID){
    treeview.call(this,treeID);
}
extend(HierarchyExpireTreeView,treeview);

//Hierarchy Tree End --------------------------------------------------------------------